﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SmartHotel.Dto;
using SmartHotel.HouseRoom.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace SmartHotel.HouseRoom
{
    [ApiExplorerSettings(GroupName = "v2")]
    public class SmartHotelAppHouseRoomService : ApplicationService, ISmartHotelAppHouseRoomService
    {
        private IRepository<SmartHotel_RoomNumberTable, int> repository;
        private IRepository<SmartHotel_HouseRoom, int> repositoryhouseroom;
        private IRepository<SmartHotel_HouseTable, int> repositoryhouse;

        public SmartHotelAppHouseRoomService(IRepository<SmartHotel_RoomNumberTable, int> repository, IRepository<SmartHotel_HouseRoom, int> repositoryhouseroom, IRepository<SmartHotel_HouseTable, int> repositoryhouse)
        {
            this.repository = repository;
            this.repositoryhouseroom = repositoryhouseroom;
            this.repositoryhouse = repositoryhouse;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultDto<CreateandUpdateHouseRoom>> PostAddHouseRoom(CreateandUpdateHouseRoom model)
        {
            var data = ObjectMapper.Map<CreateandUpdateHouseRoom, SmartHotel_HouseRoom>(model);
            await repositoryhouseroom.InsertAsync(data);
            return new ResultDto<CreateandUpdateHouseRoom> { Code = Result.Success, Message = "添加成功", Data = data };

        }
        /// <summary>
        /// 房型与房号
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<HouseRoomDto>>> GetHouseRoom()
        {
            var roomlist = await repository.GetQueryableAsync();
            var houseroomlist = await repositoryhouseroom.GetQueryableAsync();
            var houselist = await repositoryhouse.GetQueryableAsync();

            var query = (from hr in houseroomlist
                         join r in roomlist
                         on hr.RoomId equals r.Id
                         join h in houselist
                         on hr.RoomNumberId equals h.Id
                         select new HouseRoomDto()
                         {
                             Id = hr.Id,
                             RoomId = r.Id,
                             RoomNumberId = h.Id,
                             HouseId = h.Id,
                             RowHouse = hr.RowHouse,
                             RoomSort = r.RoomSort,
                             RoomRemark = r.RoomRemark,
                             RoomState = r.RoomState,
                             HouseState = r.HouseState,
                             RoomNumber = r.RoomNumber,
                             HouseName = h.HouseName,
                             HousePrice = h.HousePrice,
                             HouseSecurity = h.HouseSecurity,
                             HouseArea = h.HouseArea,
                             HousePeople = h.HousePeople,
                             HousePicture = h.HousePicture,
                             HouseType = h.HouseType,
                             ExtraBed = h.ExtraBed,
                             ExtraBedNumber = h.ExtraBedNumber,
                             HouseRuZhu = (SmartHotel_OrderState)hr.OrderState,
                             AveragePrice = h.AveragePrice,
                         }).AsQueryable();


            return new ResultDto<List<HouseRoomDto>> { Code = Result.Success, Message = "显示", Data = await query.ToListAsync() };
        }

    }
}
